<?php

/**
 * This is the model class for table "note".
 *
 * The followings are the available columns in table 'note':
 * @property integer $id
 * @property string $title
 * @property string $content
 * @property string $create_date
 * @property string $modify_date
 * @property integer $create_user_id
 * @property integer $note_book_id
 *
 * The followings are the available model relations:
 * @property NoteBook $notebook
 * @property User $owner
 */
class Note extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @return Note the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'note';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        return array(
            array('title, content', 'required'),
            array('note_book_id', 'numerical', 'integerOnly' => true),
            array('title', 'length', 'max' => 255),
            // The following rule is used by search().
            array('title, create_date, modify_date, note_book_id', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        return array(
            'notebook' => array(self::BELONGS_TO, 'NoteBook', 'note_book_id'),
            'owner' => array(self::BELONGS_TO, 'User', 'create_user_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'title' => 'Title',
            'content' => 'Content',
            'create_date' => 'Date Created',
            'modify_date' => 'Date Modified',
            'create_user_id' => 'Owner',
            'note_book_id' => 'Notebook',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        $criteria = new CDbCriteria;

        $criteria->compare('title', $this->title, true);
        $criteria->compare('create_date', $this->create_date, true);
        $criteria->compare('modify_date', $this->modify_date, true);
        $criteria->compare('note_book_id', $this->note_book_id);
        $criteria->compare('create_user_id', Yii::app()->user->id);

        return new CActiveDataProvider(get_class($this), array(
            'criteria' => $criteria,
        ));
    }

    /**
     * Set the value of attribute 'create_user_id', 'create_date', 'modify_date', and 'content' before the form is validated.
     */
    protected function beforeValidate() {
        if ($this->isNewRecord) {
            $this->create_user_id = Yii::app()->user->id;
            $this->create_date = $this->modify_date = new CDbExpression('NOW()');
        } else {
            $this->modify_date = new CDbExpression('NOW()');
        }

        return parent::beforeValidate();
    }

}